package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.CourseSchedulingDetail;
import com.xmy.cultivate.entity.Grade;
import com.xmy.cultivate.entity.LackCourseLog;
import com.xmy.cultivate.enums.StatusSchedulingEnum;
import com.xmy.cultivate.mapper.LackCourseLogMapper;
import com.xmy.cultivate.mapper.RecordCourseMapper;
import com.xmy.cultivate.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.CommonUtil;
import com.xmy.cultivate.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 缺课记录 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-03-20
 */
@Service
public class LackCourseLogServiceImpl extends ServiceImpl<LackCourseLogMapper, LackCourseLog> implements ILackCourseLogService {

    @Autowired
    LackCourseLogMapper lackCourseLogMapper;

    @Autowired
    @Lazy
    IQuarterService iQuarterService;

    @Autowired
    @Lazy
    ICourseSchedulingAlwaysService iCourseSchedulingAlwaysService;

    @Autowired
    @Lazy
    IRecordCourseService iRecordCourseService;

    @Autowired
    @Lazy
    RecordCourseMapper recordCourseMapper;

    @Autowired
    @Lazy
    IGradeService iGradeService;

    @Autowired
    ICourseSchedulingDetailService iCourseSchedulingDetailService;

    @Autowired
    IdWorker idWorker;

    @Autowired
    IStudentGradeService iStudentGradeService;

    @Override
    public boolean batchAdd(Integer yearPart, Integer quarterNum, Long schoolId, Long studentId, Long subjectsId, Integer yearClassId, Long gradeId, Integer type, Integer lackType){
        Grade grade = iGradeService.getById(gradeId);
        Integer gradeSectionId = CommonUtil.getGradeSectionId(yearClassId);
        if (gradeSectionId == null){
            return false;
        }

        LocalDateTime localDateTimeNow = LocalDateTime.now().withNano(0);

        QueryWrapper<CourseSchedulingDetail> courseSchedulingDetailQueryWrapper = new QueryWrapper<>();
        courseSchedulingDetailQueryWrapper.eq("grade_id",grade.getId());
        Integer minLessonNum = recordCourseMapper.getMinLessonNum(schoolId,studentId,subjectsId,yearPart,quarterNum,grade.getLessonType());
        if (minLessonNum>0){
            courseSchedulingDetailQueryWrapper.lt("lesson_num",minLessonNum);//小于课次
        }else {
            courseSchedulingDetailQueryWrapper.le("school_date", LocalDate.now());//小于等于时间
        }
        courseSchedulingDetailQueryWrapper.orderByAsc("school_date");
        List<CourseSchedulingDetail> courseSchedulingDetailList = iCourseSchedulingDetailService.list(courseSchedulingDetailQueryWrapper);

        QueryWrapper<LackCourseLog> lackCourseLogQueryWrapper = new QueryWrapper<>();
        for (CourseSchedulingDetail courseSchedulingDetail:courseSchedulingDetailList){
            //如果排课还未记上课，不需要添加到缺少，扣课时时为添加到到课或者请假
            if (courseSchedulingDetail.getSchoolDate().equals(LocalDate.now()) && courseSchedulingDetail.getStatus().equals(StatusSchedulingEnum.NO)){
                continue;
            }
            if(courseSchedulingDetail.getLessonNum() == null || courseSchedulingDetail.getLessonNum()>15){
                continue;
            }
            Integer count = recordCourseMapper.getStudentUpClassCountForLessonNum(schoolId,studentId,subjectsId,yearPart,quarterNum,grade.getLessonType(),courseSchedulingDetail.getLessonNum());
            if (count.equals(0)){
                //lackCourseLogQueryWrapper.eq("scheduling_always_id",courseSchedulingDetail.getId());
                lackCourseLogQueryWrapper.eq("student_id",studentId);
                lackCourseLogQueryWrapper.eq("subjects_id",subjectsId);
                lackCourseLogQueryWrapper.eq("year_part",yearPart);
                lackCourseLogQueryWrapper.eq("quarter_num",quarterNum);
                //lackCourseLogQueryWrapper.eq("grade_section",gradeSectionId);
                lackCourseLogQueryWrapper.eq("lesson_type",grade.getLessonType());
                lackCourseLogQueryWrapper.eq("lesson_num",courseSchedulingDetail.getLessonNum());
                lackCourseLogQueryWrapper.last("limit 1");
                LackCourseLog lackCourseLog = this.getOne(lackCourseLogQueryWrapper);
                if (lackCourseLog==null){
                    lackCourseLog = new LackCourseLog();
                    lackCourseLog.setId(idWorker.nextId());
                    lackCourseLog.setSchoolId(schoolId);
                    lackCourseLog.setGradeId(grade.getId());
                    lackCourseLog.setType(type);
                    lackCourseLog.setGradeSection(gradeSectionId);
                    lackCourseLog.setYearPart(yearPart);
                    lackCourseLog.setQuarterNum(quarterNum);
                    lackCourseLog.setStudentId(studentId);
                    lackCourseLog.setYearClassId(grade.getYearClassId().getKey());
                    lackCourseLog.setSubjectsId(subjectsId);
                    lackCourseLog.setLessonType(grade.getLessonType());
                    lackCourseLog.setLessonNum(courseSchedulingDetail.getLessonNum());
                    lackCourseLog.setSchedulingAlwaysId(courseSchedulingDetail.getId());
                    lackCourseLog.setLackType(lackType);
                    this.save(lackCourseLog);
                }
            }
            lackCourseLogQueryWrapper.clear();
        }
        lackCourseLogQueryWrapper.clear();
        lackCourseLogQueryWrapper.eq("student_id",studentId);
        lackCourseLogQueryWrapper.eq("subjects_id",subjectsId);
        lackCourseLogQueryWrapper.eq("year_part",yearPart);
        lackCourseLogQueryWrapper.eq("quarter_num",quarterNum);
        lackCourseLogQueryWrapper.eq("grade_section",gradeSectionId);
        lackCourseLogQueryWrapper.eq("lesson_type",grade.getLessonType());
        List<LackCourseLog> lackCourseLogList = this.list(lackCourseLogQueryWrapper);

        for (LackCourseLog lackCourseLog:lackCourseLogList){
            //已添加过缺课记录，但是没有补过课的
            if (!lackCourseLog.getIsSignIn().equals(1) && !lackCourseLog.getStatus().equals(1)){
                lackCourseLog.setGradeId(grade.getId());
                lackCourseLog.setSchoolId(grade.getSchoolId());
                lackCourseLog.setType(type);
                this.updateById(lackCourseLog);
            }
        }
        return true;
    }


    public LackCourseLog getLackCoruseLogForStudent(Integer yearPart, Integer quarterNum, Long studentId,Long subjectsId,Integer lessonNum){
        return lackCourseLogMapper.getLackCoruseLogForStudent(yearPart,quarterNum,studentId,subjectsId,lessonNum);
    }

    @Override
    public LackCourseLog getOneForId(Long Id){
        return lackCourseLogMapper.getOneForId(Id);
    }

    @Override
    public LackCourseLog getOneForOutId(Long Id){
        return lackCourseLogMapper.getOneForOutId(Id);
    }

    @Override
    public Integer updateDeleted(Long id, Integer deleted){
        return lackCourseLogMapper.updateDeleted(id,deleted);
    }
}
